package com.cyl.h5.controller;

import com.cyl.h5.config.SecurityUtil;
import com.cyl.manager.act.domain.entity.CouponActivity;
import com.cyl.manager.act.domain.entity.MemberCoupon;
import com.cyl.manager.act.domain.query.CouponActivitySy;
import com.cyl.manager.act.domain.query.MemberCouponQuery;
import com.cyl.manager.act.domain.vo.CouponActivityVO;
import com.cyl.manager.act.domain.vo.MemberCouponVO;
import com.cyl.manager.act.pojo.dto.CouponCategoryDTO;
import com.cyl.manager.act.pojo.vo.CouponCategoryVO;
import com.cyl.manager.act.service.CouponActivityNewService;
import com.cyl.manager.act.service.CouponActivityService;
import com.cyl.manager.act.service.MemberCouponService;
import com.cyl.manager.ums.domain.entity.Member;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.core.redis.RedisService;
import com.ruoyi.framework.config.LocalDataUtil;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@Api(description = "优惠券接口列表")
@RestController
@RequestMapping("/h5/coupon")
public class H5CouponController {

    @Autowired
    private CouponActivityService couponActivityService;
    
    @Autowired
    private CouponActivityNewService couponActivityNewService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private MemberCouponService memberCouponService;

    @Autowired
    private RedisCache redisCache;
    
    @ApiOperation("优惠券活动列表")
    @PostMapping("/activity/list")
    public ResponseEntity<Page<CouponActivityVO>> list(Pageable page) {
        //获取所有未过期且运行中的活动
        return ResponseEntity.ok(couponActivityService.selectListByH5(page));
    }

    @GetMapping("/activity/get")
    public ResponseEntity<CouponActivityVO> getDetail(Long id) {
        CouponActivityVO detail = couponActivityService.getDetail(id);
        return ResponseEntity.ok(detail);
    }

//    @PostMapping("/receive")
//    public ResponseEntity<Object> receiveCoupon(Long id) {
//        Long memberId = SecurityUtil.getLocalMember().getId();
//        String redisKey = "h5_receive_coupon" + id+"_"+memberId;
//        String redisValue = id + "_" + System.currentTimeMillis();
//        try {
//            redisService.lock(redisKey, redisValue, 60);
//            return ResponseEntity.ok(couponActivityService.receiveCoupon(id,memberId).get("zt"));
//        } catch (Exception e) {
//            throw new RuntimeException(e.getMessage());
//        } finally {
//            try {
//                redisService.unLock(redisKey, redisValue);
//            } catch (Exception e) {
//
//            }
//        }
//    }
    
    
    @PostMapping("/receiveByZbjScq")
    public ResponseEntity<Map<String, Object>> receiveByZbjScq(Long id) {
        Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        return ResponseEntity.ok(couponActivityNewService.receiveCouponScq(id,member));
    }
    
    
    //TODO 去掉重复领券加锁
    @PostMapping("/receiveByZbjFlq")
    public ResponseEntity<Boolean> receiveByZbjFlq(Long id) {
        Member member = (Member) LocalDataUtil.getVar(Constants.MEMBER_INFO);
        return ResponseEntity.ok(couponActivityNewService.receiveCouponByFlq(id,member));
    }
    
    //TODO 重复领券加锁
    /*
    @PostMapping("/receiveByZbjFlq")
    public ResponseEntity<Boolean> receiveByZbjFlq(Long id) {
        Long memberId = SecurityUtil.getLocalMember().getId();
        String redisKey = "h5_receive_coupon" + id+"_"+memberId;
        String redisValue = id + "_" + System.currentTimeMillis();
        try {
            redisService.lock(redisKey, redisValue, 60);
            return ResponseEntity.ok(couponActivityService.receiveCouponByFlq(id,memberId));
        } catch (Exception e) {
            throw new RuntimeException(e.getMessage());
        } finally {
            try {
                redisService.unLock(redisKey, redisValue);
            } catch (Exception e) {

            }
        }
    }*/
  
	


    @ApiOperation("优惠券活动列表")
    @PostMapping("/list")
    public ResponseEntity<Page<MemberCoupon>> list(@RequestBody MemberCouponQuery query, Pageable page) {
        //获取所有未过期且运行中的活动
        return ResponseEntity.ok(memberCouponService.selectListByH5(query, page));
    }

    
    @ApiOperation("优惠券聚合活动列表")
    @PostMapping("/couponCategorylist")
    public ResponseEntity<List<CouponCategoryDTO>> couponCategorylist(@RequestBody MemberCouponQuery query) {
        //获取所有未过期且运行中的活动
    	query.setMemberId(SecurityUtil.getLocalMember().getId());
    	if(query.getType()==null) {
    		query.setType(1);
    	}
        return ResponseEntity.ok(memberCouponService.selectListGrepByH5(query));
    }
    
    
  

    /**
     * 根据购买的商品筛选可用优惠券
     *
     * @param query
     * @return
     */
    @PostMapping("/can/use/list")
    public ResponseEntity<List<MemberCoupon>> getCanUseList(@RequestBody MemberCouponQuery query) {
        return ResponseEntity.ok(memberCouponService.getCanUseList(query.getProducts()));
    }

}
